package com.example.demo.rocketmq;

import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

public class SimpleProducerTest {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer defaultMQProducer = new DefaultMQProducer("pruducer-test");
        defaultMQProducer.setNamesrvAddr(RocketMqConfig.NAME_SERVER);
        defaultMQProducer.start();
        for (int i=0; i<5;i++
             ) {
            StudentMessage studentMessage = new StudentMessage("test" + i, "message" + i);
            SendResult send = defaultMQProducer.send(new Message(RocketMqConfig.TOPIC, JSONObject.toJSONString(studentMessage).getBytes()));
            System.out.println(">>>>>>>>>>"+i+JSONObject.toJSONString(send));
        }

        defaultMQProducer.shutdown();

    }

    static class StudentMessage{
        public StudentMessage(String studentName, String message) {
            this.studentName = studentName;
            this.message = message;
        }

        private String studentName;

        private String message;

        public String getStudentName() {
            return studentName;
        }

        public void setStudentName(String studentName) {
            this.studentName = studentName;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}
